package com.efast.dmom.aps.datahandlers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.aps.bean.Supplier;
import com.efast.dmom.aps.dao.SupplierDao;
import com.efast.dmom.aps.util.SpringWebUtils;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * 
 * @author WJ
 * @date 2020/2/20
 *
 */
@Component
public class SupplierDataHandler {

	@Autowired
	private SupplierDao supplierDao;

	public void update(Supplier obj) {

		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			//obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			supplierDao.update(obj.getInnerData());
		}
	}

	public Supplier getById(long id) {
		Supplier supplier = new Supplier();
		supplier.setInnerData(supplierDao.getById(id));
		return supplier;
	}

	public Supplier getByName(String name) {
		Supplier supplier = new Supplier();
		supplier.setInnerData(supplierDao.getByName(name));
		return supplier;
	}

	// get supplier by code
	public Supplier getSupplierByCode(String supplierCode) {
		Supplier supplier = new Supplier();
		supplier.setInnerData(supplierDao.getSupplierByCode(supplierCode));
		return supplier;
	}

	public void save(Supplier obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		    obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//		    obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			supplierDao.save(obj.getInnerData());
		}
	}

	public void delete(Supplier obj) {
		if(obj.getInnerData() != null){

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			//obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			
			supplierDao.delete(obj.getInnerData());
		}
		
	}

}
